package com.dtm.ui;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.TextView;

import com.dtm.R;
import com.dtm.model.User;

public class MainActivity extends Activity {
	
	Handler handler = new Handler();
	
	private ImageButton ibtnChange;
	private Button btnHost;
	private Button btnJoin;
	private TextView txtName;
	
	public static String userName = "Default User";
	public static int userAvatar = 0;
	
	private User user = new User();
	
	public static final int REQUEST_CODE_CHANGE = 101;
	public static final int RESULT_CODE_SAVE = 102;
	
	public static final int HOST_ACTIVITY = 0;
	public static final int CLIENT_ACTIVITY = 1;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		setButton();
	}

	private void setButton() {
		btnJoin = (Button) findViewById(R.id.btn_Join);
		btnJoin.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				Intent intent = new Intent(MainActivity.this, SearchHost.class);
				startActivity(intent);
			}
		});
		
		btnHost = (Button) findViewById(R.id.btn_Host);
		btnHost.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				Intent intent = new Intent(MainActivity.this, InRoomActivity.class);
				intent.putExtra("key", HOST_ACTIVITY);
				startActivity(intent);
			}
		});
		
		ibtnChange = (ImageButton) findViewById(R.id.ibtn_Change);
		ibtnChange.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				Intent intent = new Intent(MainActivity.this, ChangeInfo.class);
				startActivityForResult(intent, REQUEST_CODE_CHANGE);
			}
		});		
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		if(requestCode == REQUEST_CODE_CHANGE){
			String name = data.getStringExtra("name");
			userName = name;
			int avatar = data.getIntExtra("avatar", 0);
			txtName = (TextView) findViewById(R.id.txt_Name);
			txtName.setText(name);
			user.setName(name);
			user.setAvatar(avatar);
			userAvatar = user.getAvatar();
			user.intToID();
			userName = user.getAvatar() + userName;
			ibtnChange.setImageResource(user.getIdAvt());
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}
}
